﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;

namespace Bing
{
    /// <summary>
    /// Contains properties specific to a set of results returned for a request that includes the News <see cref="ApiHelper.SourceType"/>.
    /// </summary>
    [DebuggerDisplay("Title: {Title} - {Url}")]
    public class NewsResult
    {
        // TODO: Documentation Error: http://msdn.microsoft.com/en-us/library/dd250996.aspx, Data Type is incorrect. Should be UInt32 or Bool
        /// <summary>
        /// Identifies this result as "breaking news" or not "breaking news."
        /// </summary>
        /// <value>Identifies this result as "breaking news" or not "breaking news."</value>
        /// <remarks>
        /// <ul>
        /// <li>0 = not breaking news</li>
        /// <li>1 = breaking news</li>
        /// </ul>
        /// </remarks>
        public bool BreakingNews { get; set; }

        // TODO: Documentation Error: http://msdn.microsoft.com/en-us/library/dd250945.aspx, 
        /// <summary>
        /// Gets or sets the date on which this result was indexed.
        /// </summary>
        /// <value>The date on which this result was indexed.</value>
        public DateTime Date { get; set; }

        /// <summary>
        /// Gets or sets a List of <see cref="NewsCollection"/> results returned for this <see cref="NewsResult"/>, analogous to news clusters on <a href="http://bing.com/news">bing.com/news</a>.
        /// </summary>
        /// <value>A List of <see cref="NewsCollection"/>s items.</value>
        public NewsCollection NewsCollections { get; set; }

        /// <summary>
        /// Gets or sets a representative sample of the news.
        /// </summary>
        /// <value>A representative sample of the news.</value>
        public string Snippet { get; set; }

        /// <summary>
        /// Gets or sets the organization responsible for this result.
        /// </summary>
        /// <value>The organization responsible for this result.</value>
        public string Source { get; set; }

        /// <summary>
        /// Gets or sets the headline for this result.
        /// </summary>
        /// <value>The headline for this result.</value>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the URL of the website hosting this result.
        /// </summary>
        /// <value>The URL of the website hosting this result.</value>
        public string Url { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="NewsResult"/> class.
        /// </summary>
        /// <param name="element">The element.</param>
        public NewsResult(XContainer element)
        {
            if (element == null) return;

            Title = ApiHelper.GetElementValueOrDefault<string>(element, "Title", ApiHelper.NewsNs);
            Url = ApiHelper.GetElementValueOrDefault<string>(element, "Url", ApiHelper.NewsNs);
            Source = ApiHelper.GetElementValueOrDefault<string>(element, "Source", ApiHelper.NewsNs);
            Date = ApiHelper.GetElementValueOrDefault<DateTime>(element, "Date", ApiHelper.NewsNs);
            Snippet = ApiHelper.GetElementValueOrDefault<string>(element, "Snippet", ApiHelper.NewsNs);
            string breakingNews = ApiHelper.GetElementValueOrDefault<string>(element, "BreakingNews", ApiHelper.NewsNs);
            BreakingNews = (breakingNews == "1");
            XElement collection = element.Element(XName.Get("NewsCollections", ApiHelper.NewsNs));
            if (collection == null) return;

            XElement newsCollections = collection.Element(XName.Get("NewsCollection", ApiHelper.NewsNs));
            if (newsCollections == null) return;

            NewsCollections = NewsCollection.ParseElement(newsCollections);

        }

    }
}
